package cn.com.oceansoft.osc.ms.controller;

import cn.com.oceansoft.osc.ms.schedule.entity.parasalarmresult;
import cn.com.oceansoft.osc.ms.utils.QueryUtil;
import cn.com.oceansoft.osc.ms.utils.XMLUtil;
import cn.com.oceansoft.osc.ms.service.IQueryService;
import cn.ucox.web.framework.view.ApiResult;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by caobo on 2017/7/8.
 */
//@Controller
//@RequestMapping(value = "queryList")
public class QueryController {

    @Resource
    IQueryService iQueryService;

    @ResponseBody
    @RequestMapping(value = "version", method = GET)
    public ApiResult version()
    {
        String version="0.01";
        return  new ApiResult<Object>().success(version);
    }

    /**
     * 查询资源服务平台数据接口
     *功能描述：写资源服务平台通用查询方法
     * @param
     * @return
     */
    @ResponseBody()
    @RequestMapping(value = "QueryResult", method = POST)
    public ApiResult  QueryResult(@RequestBody Map<String, String> map) {
        QueryUtil queryUtil=new QueryUtil();
        Map<String, String> result = new HashMap<>();
        String tablename="";
        String rowlist="";//多个字段名称用,分开
        String wherestr="";//多个查询条件用,分开
        String orderstr="";//多个排序用,分开
        String isdistinct="false";//是否去重
        if (!StringUtils.isBlank(map.get("tablename"))) {
            tablename = map.get("tablename");
        } else {
            result.put("sucess", "F");
            result.put("errorCode", "tablename不能为空");
            return new ApiResult().failure(result);
        }
        if (!StringUtils.isBlank(map.get("rowlist"))) {
            rowlist = map.get("rowlist");
        } else {
            result.put("sucess", "F");
            result.put("errorCode", "rowlist不能为空");
            return new ApiResult().failure(result);
        }
        wherestr = map.get("wherestr");
        orderstr = map.get("orderstr");
        if (!StringUtils.isBlank(map.get("limitstr"))) {
            rowlist = map.get("limitstr");
        } else {
            result.put("sucess", "F");
            result.put("errorCode", "limitstr不能为空");
            return new ApiResult().failure(result);
        }
        isdistinct = map.get("isdistinct");
        queryUtil.setTablename(tablename);
        queryUtil.setRowlist(rowlist);
        queryUtil.setWherestr(wherestr);
        queryUtil.setOrderstr(orderstr);
        queryUtil.setTablename(tablename);
        queryUtil.setIsdistinct(isdistinct);
        Map<String, String> map1 = new HashMap<>();
        String map1json="";
        try {
            map1 = iQueryService.getQueryResult(queryUtil);
            map1json=JSONObject.toJSONString(map1);
        }
        catch (Exception ex){
            result.put("sucess", "F");
            result.put("errorCode", ex.getMessage());
            return new ApiResult().failure(result);
        }
        if (StringUtils.isBlank(map1json)){
        result.put("sucess", "F");
        result.put("errorCode", "MAP转String报错");
        return new ApiResult().failure(result);
        }
        else
        {
        result.put("sucess", "T");
        result.put("errorCode", "");
        result.put("datajson", map1json);
        return new ApiResult().success(result);
        }
    }
}
